<template>
	<view class="cq-container">
		<customImage :imgSrc="'/clb/clb/108/top.png'" width="750rpx"></customImage>
		<view class="form-container">
			<uni-forms class="forms-item" ref="accountForm" :model="formData">
				<uni-forms-item>
					<input style="width: 600rpx" maxlength="11" placeholder="请输入手机号" type="tel"
						v-model="formData.mobile" class="uni-input-input"
						placeholder-style="color: #0D0D0D;font-size:32rpx" />
				</uni-forms-item>
				<view class="input-between">
					<uni-forms-item>
						<input style="width: 262rpx; margin-left: 50rpx" maxlength="6" type="tel"
							v-model="formData.smsCode" placeholder="请输入验证码" class="uni-input-input"
							placeholder-style="color: #0D0D0D;font-size:32rpx" />
					</uni-forms-item>
					<uni-forms-item class="btn-bg">
						<view class="code" @click="sendCode" :style="{ cursor: isSend ? 'not-allowed' : '' }">
							{{ isSend ? `重新发送 ${isNum}` : '获取验证码' }}
						</view>
					</uni-forms-item>
				</view>

				<view class="login-btn" style="text-align: center">
					<customImage :imgSrc="'/clb/clb/108/orderBtn.png'" width="616rpx" @click="submitform"></customImage>
				</view>
				<view
					style="margin-top: 20rpx; margin-bottom: 0rpx; padding-bottom: 0rpx; display: flex; align-items: center">
					<checkbox-group @change="changeAll">
						<label>
							<checkbox value="cb" :checked="allChecked" style="transform: scale(0.7)" />
							<text style="color: #333333; font-weight: 400; font-size: 24rpx">已阅读并同意</text>
						</label>
					</checkbox-group>
					<text style="color: #f65814; font-weight: 400; font-size: 24rpx" @click="privatePro">《产品详情》</text>
					<text style="color: #333333; font-weight: 400; font-size: 24rpx">和</text>
					<text style="color: #f65814; font-weight: 400; font-size: 24rpx"
						@click="privateMse">《个人信息保护政策》</text>
				</view>
			</uni-forms>
		</view>

		<customImage :imgSrc="'/clb/clb/108/bottom.png'"></customImage>
		<view class="arrive-btn" :style="{ display: showHeight > 800 ? '' : 'none' }">
			<customImage :imgSrc="'/clb/clb/108/orderBtn.png'" width="616rpx" @click="toTop"></customImage>
		</view>
		<uni-popup ref="popupOk" type="bottom" border-radius="10px 10px 0 0">
			<per-info agreementType="1" @ok="ok"></per-info>
		</uni-popup>
		<uni-popup ref="popupPro" type="bottom" border-radius="10px 10px 0 0">
			<per-info agreementType="2" @ok="ok1"></per-info>
		</uni-popup>
		<!-- 二次确认 -->
		<uni-popup ref="twoSurePop" type="bottom" border-radius="10px 10px 0 0">
			<view class="sure-image">
				<customImage :imgSrc="'/clb/clb/108/twoSure.png'" width="640rpx"></customImage>
			</view>
			<view class="cancel-btn">
				<customImage :imgSrc="'/clb/clb/108/no.png'" width="280rpx" @click="cancelBtn"></customImage>
				<customImage :imgSrc="'/clb/clb/108/ok.png'" width="280rpx" @click="successOk"></customImage>
			</view>
		</uni-popup>
		<uni-popup ref="resultPop" type="bottom" border-radius="10px 10px 0 0">
			<view class="sure-image">

				<customImage :imgSrc="'/clb/clb/108/result.png'" width="580rpx"></customImage>
			</view>
			<view class="close-sure-btn">
				<customImage :imgSrc="'/clb/clb/108/sureOk.png'" width="380rpx" @click="closeSureBtn"></customImage>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		watchEffect,
		watch
	} from 'vue';
	import {
		onLoad,
		onPageScroll,
		onReady
	} from '@dcloudio/uni-app';
	import initApi from '@/apis/init.js';
	import perInfoVue from '@/components/per-info/per-info.vue';

	const accountForm = ref();
	// 表单数据
	const showHeight = ref(0);
	const isSend = ref(false);
	const allChecked = ref(false);
	const popupOk = ref();
	const popupPro = ref();
	const twoSurePop = ref();
	const resultPop = ref();
	const isNum = ref(0);
	const isTimer = ref();
	const agree = ref('同意');

	const formData = reactive({
		mobile: '',
		smsCode: null,
		orderNo: null,
		channelCode: '',
		productCode: ''
	});

	const changeAll = (val) => {
		if (val.detail.value.length > 0) {
			allChecked.value = true;
		} else {
			allChecked.value = false;
		}
	};

	watch(
		() => formData.mobile,
		(newVal, oldValue) => {
			const isOkMobile = /^1[3-9][0-9]\d{8}$/.test(newVal);
			if (isOkMobile) {
				uni.hideKeyboard();
				sendCode();
			}
		}
	);
	const sendCode = async () => {
		if (!formData.mobile) return uni.utils.toast('请输入手机号！');
		if (isSend.value) return;
		try {
			const arr = {
				mobile: formData.mobile,
				channelCode: formData.channelCode,
				productCode: formData.productCode
			};
			const isOkMobile = /^1[3-9][0-9]\d{8}$/.test(formData.mobile);
			if (!isOkMobile) return uni.utils.toast('请输入正确的手机号');
			const res = await initApi.initInventoryPackageOrder(arr);
			if (res.code != 20000) {
				return uni.utils.toast(res.message)
			};
			formData.orderNo = res.data.orderNo;
			isNum.value = 60;
			isSend.value = true;
			isTimer.value = setInterval(() => {
				if (isNum.value <= 0) {
					clearInterval(isTimer.value);
					isNum.value = 0;
					isSend.value = false;
				}
				isNum.value--;
			}, 1000);
			return uni.utils.toast('验证码发送成功');
		} catch (e) {
			//TODO handle the exception
			isSend.value = false;
			isNum.value = 0;
			clearInterval(isTimer.value);
			uni.utils.toast('请输入正确手机号');
		}
	};
	const cancelBtn = () => {
		twoSurePop.value.close();
	};
	const closeSureBtn = () => {
		resultPop.value.close();
	};
	const successOk = async () => {
		try {
			if (!allChecked.value) return uni.utils.toast('请勾选《个人信息收集》');
			if (!formData.mobile) return uni.utils.toast('请输入手机号');
			if (!formData.smsCode) return uni.utils.toast('请输入验证码');
			if (!formData.orderNo) return uni.utils.toast('订单号为空 请输入正确信息');
			const arr = {
				orderNo: formData.orderNo,
				mobile: formData.mobile,
				smsCode: formData.smsCode,
				channelCode: formData.channelCode,
				productCode: formData.productCode
			};
			const res = await initApi.processInventoryPackageOrder(arr);
			if (res.code != 20000) {
				return uni.utils.toast(res.message);
			}
			cancelBtn();
			clearInterval(isTimer.value);
			isNum.value = 0;
			isSend.value = false;
			nextTick(() => {
				resultPop.value.open('center');
			});
		} catch (e) {
			//TODO handle the exception
			console.log(e);
			clearInterval(isTimer.value);
			isNum.value = 0;
			isSend.value = false;
			return uni.utils.toast('订单订购失败');
		}
	};
	/* 弹开二次确认面板 */
	const submitform = async () => {
		if (!allChecked.value) return uni.utils.toast('请勾选《个人信息收集》');
		if (!formData.mobile) return uni.utils.toast('请输入手机号');
		if (!formData.smsCode) return uni.utils.toast('请输入验证码');
		if (!formData.orderNo) return uni.utils.toast('订单号为空 请输入正确信息');
		twoSurePop.value.open('center');
	};

	onLoad((query) => {
		if (!query.a || !query.p) {
			uni.reLaunch({
				url: '/pages/clb/404'
			});
		}

		formData.channelCode = query.a;
		formData.productCode = query.p;
	});
	const toTop = () => {
		uni.pageScrollTo({
			scrollTop: 0,
			duration: 300
		});
	};
	onPageScroll((e) => {
		showHeight.value = e.scrollTop;
	});
	const ok = () => {
		popupOk.value.close();
	};
	const ok1 = () => {
		popupPro.value.close();
		allChecked.value = true;
	};
	const privateMse = () => {
		popupOk.value.open('center');
	};
	const privatePro = () => {
		popupPro.value.open('center');
	};
</script>

<style lang="scss" scoped>
	.arrive-btn {
		position: fixed;
		bottom: 20rpx;
		width: 750rpx;
		text-align: center;
	}

	.cq-container {
		width: 750rpx;
		margin: 0 auto;
		background-color: #93e0fe;
		box-sizing: border-box;
		overflow-x: hidden;

		/* 表单 */
		.form-container {
			height: 540rpx;
			width: 620rpx;
			margin: 0 auto;
		}

		.login-btn {
			animation: scaleUp 2.5s infinite;
			/* 动画名称、持续时间、是否无限循环 */
		}

		@keyframes scaleUp {

			0%,
			50% {
				transform: scale(1);
				/* 开始时的缩放比例 */
			}

			25%,
			75% {
				transform: scale(1.05);
				/* 缩放比例最大值 */
			}

			100% {
				transform: scale(1);
				/* 结束时的缩放比例 */
			}
		}

		.uni-forms-item {
			height: 80rpx;
			margin-bottom: 20 !important;
			border-bottom: 2rpx solid #eee;
			background-color: #ffffff;
			border-radius: 100rpx;
			border: 2rpx solid #3390ff;
			box-sizing: border-box;

			.uni-input-input {
				width: 616rpx;
				margin-left: 50rpx;
			}

			::v-deep .uni-forms-item__error {
				width: 100%;
				padding-top: 10rpx;
				color: #fff;
				text-align: center;
				font-size: 28rpx;
				transition: none;
			}
		}

		.input-between {
			display: flex;
			justify-content: space-between;

			.btn-bg {
				background-color: #3390ff;
			}

			.code {
				width: 280rpx;
				text-align: center;
				color: #ffffff;
				line-height: 80rpx;
			}
		}
	}

	.cancel-btn {
		display: flex;
		margin-top: 33rpx;
		justify-content: space-between;
	}

	.close-sure-btn {
		margin-top: 54rpx;
		display: flex;
		justify-content: center;
	}

	::v-deep {
		image {
			vertical-align: bottom; // 解决image背景图片下方有10rpx 的白边
		}
	}

	:deep(.uni-checkbox-input) {
		border-radius: 80rpx !important;
	}
</style>